package com.amir.common.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.core.content.FileProvider
import java.io.File

/**
 * @author justkeepmoving.
 * @date 2019/7/26 10:53.
 * make your program better than last time.
 */
object FileProviderUtils {
    fun getUriForFile(context: Context, file: File?): Uri {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            FileProvider.getUriForFile(
                context,
                context.packageName + ".FileProvider",
                file!!
            )
        } else {
            Uri.fromFile(file)
        }
    }

    fun getFileUri(context: Context, file: File?): Uri {
        return FileProvider.getUriForFile(context, context.packageName + ".FileProvider", file!!)
    }

    fun revokePermission(context: Context, uri: Uri?) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            context.revokeUriPermission(
                uri,
                Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
            )
        }
    }
}